package com.example.module_word.activity;

import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.bdy.voice.util.VoicecompoundTool;
import com.blankj.utilcode.util.ToastUtils;
import com.example.module_word.R;
import com.example.module_word.data.DanCiData;
import com.example.module_word.databinding.ActivityDanCiBinding;
import com.example.module_word.room.Danci;
import com.example.module_word.room.entity.MyWord;
import com.fenghuajueli.libbasecoreui.baseswitch.SwitchKeyUtils;
import com.fenghuajueli.libbasecoreui.user.UserInfoUtils;
import com.fenghuajueli.libbasecoreui.utils.AdUtil;
import com.fenghuajueli.libbasecoreui.utils.MyStatusBarUtils;
import com.fenghuajueli.libbasecoreui.utils.QuickClickUtils;
import com.sy.module_ad_switch_manager.AdSwitchManger;
import java.util.List;

public class DanCiActivity extends AppCompatActivity implements View.OnClickListener {

    private ActivityDanCiBinding binding;
    private List<Danci> list;
    private String kind;
    private int index=1;//表示单前选中
    private String number;
    private final String NewInteractionAdTag = "NewInteractionAdTag";
    private VoicecompoundTool bdy;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityDanCiBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        MyStatusBarUtils.setStatusBar(this,"#00fff000");
        bindListener();
        kind = getIntent().getStringExtra("kind");
        number = getIntent().getStringExtra("jrdc");
        AdUtil.showBannerAd(this,binding.adFrameLayout,"DanCiActivity_"+kind+number);
        list = DanCiData.getDanCiByKind(kind);
        initView();

        if (!DanCiData.isMyWord(list.get(index).word)){
            DanCiData.insertMyWord(new MyWord(list.get(index).kind,list.get(index).word,list.get(index).phonetic,list.get(index).desc));
        }

        bdy = new VoicecompoundTool(this);
        bdy.initbdyKey("115791963", "com.wyf.lyf_voice");
    }

    private void initView() {
        binding.danciNumber.setText(index+"/"+number);
        binding.danci.setText(list.get(index).word);
        binding.danciSoundmark.setText(list.get(index).phonetic);
    }

    private void bindListener() {
        binding.danciBack.setOnClickListener(this);
        binding.danciNext.setOnClickListener(this);
        binding.danciUp.setOnClickListener(this);
        binding.danciHintBtn.setOnClickListener(this);
        binding.llDc.setOnClickListener(this);
        binding.danciFhsy.setOnClickListener(this);
        binding.danciZlyc.setOnClickListener(this);
    }


    @Override
    protected void onResume() {
        super.onResume();
        AdSwitchManger.loadInteractionAd(true, SwitchKeyUtils.getAdStatus(), UserInfoUtils.getInstance().isVip(), this, NewInteractionAdTag);
    }

    @Override
    public void onClick(View v) {
        if (QuickClickUtils.isFastClick()){
            return;
        }
        int id = v.getId();
        if (id == R.id.danci_back || id == R.id.danci_fhsy){
            //返回
            onBackPressed();
        }else if (id == R.id.danci_next){
            AdUtil.clickAdAndPayWithIncentiveMode(this,1,1,() -> {
                //下一个
                index++;
                if (index<=Integer.parseInt(number)){
                    initView();
                    if (!DanCiData.isMyWord(list.get(index).word)){
                        DanCiData.insertMyWord(new MyWord(list.get(index).kind,list.get(index).word,list.get(index).phonetic,list.get(index).desc));
                    }
                }else {
                    index--;
                    binding.danciFinish.setVisibility(View.VISIBLE);
                    binding.danciNext.setEnabled(false);
                    //增加一次签到次数
                    DanCiData.updateJiHuaDayByKind(kind);
                    if (!DanCiData.isMyWord(list.get(index).word)){
                        DanCiData.insertMyWord(new MyWord(list.get(index).kind,list.get(index).word,list.get(index).phonetic,list.get(index).desc));
                    }
                    return null;
                }
                return null;
            });


        }else if (id == R.id.danci_up){
            AdUtil.clickAdAndPayWithIncentiveMode(this,1,1,() -> {
                //上一个
                if (index==1){
                    ToastUtils.showShort("已经是第一个了");
                    return null;
                }
                index--;
                initView();
                return null;
            });
        }else if (id == R.id.danci_hint_btn){
            AdUtil.clickAdAndPayWithIncentiveMode(this,1,1,() -> {
                //提示
                String hint = list.get(index).desc;
                hint = hint.replaceAll("n","\nn");
                hint = hint.replaceAll("vt","\nvt");
                hint = hint.replaceAll("adj","\nadj");
                hint = hint.replaceAll("vi","\nvi");
                binding.danciHint.setText(hint);
                binding.danciHint.setVisibility(View.VISIBLE);
                return null;
            });
        }else if (id == R.id.ll_dc){

            bdy.speak(list.get(index).word);
            //只要播放过，就相当于学习了，就添加到我的单词
            if (!DanCiData.isMyWord(list.get(index).word)){
                DanCiData.insertMyWord(new MyWord(list.get(index).kind,list.get(index).word,list.get(index).phonetic,list.get(index).desc));
            }
        }else if (id == R.id.danci_zlyc){
            //再来一次
            index=1;
            initView();
            binding.danciFinish.setVisibility(View.GONE);
            binding.danciNext.setEnabled(true);
        }
        //上一个或下一个都要隐藏提示
        if (id == R.id.danci_next||id == R.id.danci_up||id == R.id.danci_zlyc){
            binding.danciHint.setVisibility(View.GONE);
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        bdy.release();
    }
}